package com.jar.exercise.网络编程.URL编程;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

/**
 * 定义客户端和服务器端相互访问
 */
class Client {
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://192.168.234.1:12000/");
		String host = url.getHost();
		System.out.println(host);
		//获取此 URL的主机名（如适用）。
		String path = url.getPath();
		System.out.println(path + "*********");
		//获取此 URL的路径部分。
		int port = url.getPort();
		System.out.println(port);
		//获取此 URL的端口号。
		String protocol = url.getProtocol();
		//获取此 URL的协议名称。

		System.out.println(protocol);
		Socket socket = new Socket(host, port);
		InputStream input = socket.getInputStream();
		byte[] b = new byte[1024];
		int read = input.read(b);
		System.out.println(new String(b, 0, read));
	}
}

class Server {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(12000);
		Socket socket = serverSocket.accept();
		String hostAddress = socket.getInetAddress().getHostAddress();
		System.out.println(hostAddress);
		OutputStream outputStream = socket.getOutputStream();
		outputStream.write("你好呀：,".getBytes());
		socket.close();
		serverSocket.close();
	}
}
